package org.budo.support.spring.context.aware;

import lombok.extern.slf4j.Slf4j;
import org.budo.support.spring.util.SpringUtil;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 * @author lmw
 */
@Slf4j
public abstract class RootApplicationContextRefreshedEventListener implements ApplicationListener<ApplicationEvent> {
    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        boolean isRootApplicationContextRefreshedEvent = SpringUtil.isRootApplicationContextRefreshedEvent(event);
        if (isRootApplicationContextRefreshedEvent) {
            this.onRootApplicationContextRefreshedEvent((ContextRefreshedEvent) event);
            return;
        }

        boolean isContextRefreshedEvent = SpringUtil.isContextRefreshedEvent(event);
        if (isContextRefreshedEvent) {
            this.onContextRefreshedEvent((ContextRefreshedEvent) event);
            return;
        }
    }

    protected void onContextRefreshedEvent(ContextRefreshedEvent contextRefreshedEvent) {
        log.info("#31 onContextRefreshedEvent, contextRefreshedEvent=" + contextRefreshedEvent);
    }

    protected void onRootApplicationContextRefreshedEvent(ContextRefreshedEvent contextRefreshedEvent) {
        log.info("#35 onRootApplicationContextRefreshedEvent, contextRefreshedEvent=" + contextRefreshedEvent);
    }
}
